﻿/*
 *    Copyright [2010] [Daniel Sawan]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.kikoulol.jarmory.extractor.xml;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.kikoulol.jarmory.extractor.enums.Regions;
import com.kikoulol.jarmory.extractor.enums.LadderTypes;

public class ArenaLadder extends AbstractArmoryXmlFile
{
  private String globalSearch;
  private String lang;
  private String requestQuery;
  private String requestUrl;
  private String battleGroup;
  private String filterField;
  private String filterValue;
  private String level;
  private String maxPage;
  private String page;
  private String sortDir;
  private String sortField;
  private String teamSize;
  private String url;
  private List<TeamInfo> arenaTeams = new ArrayList<TeamInfo>();

  /**
   * 
   * @param region
   * @param battleGroupName
   * @param ladderType
   * @param pageNumber
   */
  public ArenaLadder(Regions region, String battleGroupName, LadderTypes ladderType, int pageNumber)
  {
    super(region, "arena-ladder.xml?p=" + pageNumber + "&ts=" + ladderType.value() + "&b=" + battleGroupName);
  }

  /**
   * 
   */
  protected void initProperties()
  {
    // Param globale de la Page
    this.globalSearch = this.getXml().getAttributeNode("globalSearch").getValue();
    this.lang  =        this.getXml().getAttributeNode("lang").getValue();
    this.requestQuery = this.getXml().getAttributeNode("requestQuery").getValue();
    this.requestUrl =   this.getXml().getAttributeNode("requestUrl").getValue();

    // Param globale de l'arenaLadderPageResult
    Node arenaLadderNode = this.getXml().getElementsByTagName("arenaLadderPagedResult").item(0);

    this.battleGroup =  this.getNamedItemValue(arenaLadderNode, "battleGroup");
    this.filterField =  this.getNamedItemValue(arenaLadderNode, "filterField");
    this.filterValue =  this.getNamedItemValue(arenaLadderNode, "filterValue");
    this.level =        this.getNamedItemValue(arenaLadderNode, "level");
    this.maxPage =      this.getNamedItemValue(arenaLadderNode, "maxPage");
    this.page =         this.getNamedItemValue(arenaLadderNode, "page");
    this.sortDir =      this.getNamedItemValue(arenaLadderNode, "sortDir");
    this.sortField =    this.getNamedItemValue(arenaLadderNode, "sortField");
    this.teamSize =     this.getNamedItemValue(arenaLadderNode, "teamSize");
    this.url =          this.getNamedItemValue(arenaLadderNode, "url");
  }

  /**
   * 
   * @return
   */
  public List<TeamInfo> getArenaTeams()
  {
    super.setupXml();
    
    NodeList teamsXml = this.getXml().getElementsByTagName("arenaTeam");
    for (int i = 0; i < teamsXml.getLength(); i++)
    {
      Node arenaTeamNode = teamsXml.item(i);
      String teamUrl = arenaTeamNode.getAttributes().getNamedItem("teamUrl").getNodeValue();
      TeamInfo arenaTeam = new TeamInfo(this.getRegion(), teamUrl);
      this.arenaTeams.add(arenaTeam);
    }
    return arenaTeams;
  }

  public String getBattleGroup()
  {
    super.setupXml();
    return battleGroup;
  }

  public String getFilterField()
  {
    super.setupXml();
    return filterField;
  }

  public String getFilterValue()
  {
    super.setupXml();
    return filterValue;
  }

  public String getGlobalSearch()
  {
    super.setupXml();
    return globalSearch;
  }

  public String getLang()
  {
    super.setupXml();
    return lang;
  }

  public String getLevel()
  {
    super.setupXml();
    return level;
  }

  public String getMaxPage()
  {
    super.setupXml();
    return maxPage;
  }

  public String getPage()
  {
    super.setupXml();
    return page;
  }

  public String getRequestQuery()
  {
    super.setupXml();
    return requestQuery;
  }

  public String getRequestUrl()
  {
    super.setupXml();
    return requestUrl;
  }

  public String getSortDir()
  {
    super.setupXml();
    return sortDir;
  }

  public String getSortField()
  {
    super.setupXml();
    return sortField;
  }

  public String getTeamSize()
  {
    super.setupXml();
    return teamSize;
  }

  public String getUrl()
  {
    super.setupXml();
    return url;
  }
}
